namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class PaintLineTrend : PaintLineBase
    {
        public PaintLineTrend()
        {
            base.LineType = PaintLineType.Trend;
            base.PlName_zh = "两点直线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num4 = base.CgInstance.GetX(num2);
                PaintLineBase.PointValue value4 = base.PointList[0];
                float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
                PaintLineBase.PointValue value5 = base.PointList[1];
                float num6 = base.CgInstance.GetY(base.OwnedPanelID, value5.Value, null);
                if (this.LineParam() != null)
                {
                    if ((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f))))
                    {
                        base.MoveType = 1;
                    }
                    else if ((crossHairPoint.X >= (num4 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num4 + (base.LineWidth * 2.5f))))
                    {
                        base.MoveType = 2;
                    }
                }
                else if ((crossHairPoint.Y >= (num5 - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num5 + (base.LineWidth * 5))))
                {
                    base.MoveType = 3;
                }
                else if ((crossHairPoint.Y >= (num6 - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num6 + (base.LineWidth * 5))))
                {
                    base.MoveType = 4;
                }
                if (base.MoveType == 0)
                {
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                else
                {
                    base.CgInstance.Cursor = Cursors.SizeWE;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value2 = pList[0];
                float num = (base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value3 = pList[1];
                float num2 = (base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                double[] numArray = this.LineParam(pList);
                double num5 = 0.0;
                double num6 = 0.0;
                if (numArray != null)
                {
                    num5 = numArray[0];
                    num6 = numArray[1];
                }
                float num7 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num8 = base.CgInstance.GetX(num4) - base.CgInstance.LeftYScaleWidth;
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num7 - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num8 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                float leftYScaleWidth = base.CgInstance.LeftYScaleWidth;
                float num10 = (float) ((leftYScaleWidth * num5) + num6);
                float num11 = base.CgInstance.GetWorkSpaceX() + base.CgInstance.LeftYScaleWidth;
                float num12 = (float) ((num11 * num5) + num6);
                g.DrawLine(pen, (float) (leftYScaleWidth - base.CgInstance.LeftYScaleWidth), (float) ((num10 - div.Rect.Y) - div.TitleHeight), (float) (num11 - base.CgInstance.LeftYScaleWidth), (float) ((num12 - div.Rect.Y) - div.TitleHeight));
                pen.Dispose();
                brush.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                if ((mouseOverIndex >= 0) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                {
                    int num3 = mouseOverIndex;
                    int num4 = num3 - 1;
                    if (num4 >= 0)
                    {
                        ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                        DataRow row = base.CgInstance.DataSource.Rows[num4];
                        DataRow row2 = base.CgInstance.DataSource.Rows[num3];
                        string key = row[base.CgInstance.CalenderScaleField].ToString();
                        string str2 = row2[base.CgInstance.CalenderScaleField].ToString();
                        base.PointList.Clear();
                        double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                        base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                        base.PointList.Add(new PaintLineBase.PointValue(1, str2, valueByPoint));
                        return true;
                    }
                }
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                double[] numArray = this.LineParam();
                if (numArray != null)
                {
                    if ((((crossHairPoint.Y - (numArray[0] * crossHairPoint.X)) - numArray[1]) >= (base.LineWidth * -5)) && (((crossHairPoint.Y - (numArray[0] * crossHairPoint.X)) - numArray[1]) <= (base.LineWidth * 5)))
                    {
                        return true;
                    }
                }
                else
                {
                    PaintLineBase.PointValue value2 = base.PointList[0];
                    float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                    PaintLineBase.PointValue value3 = base.PointList[1];
                    float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                    PaintLineBase.PointValue value4 = base.PointList[0];
                    int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                    float x = base.CgInstance.GetX(pos);
                    if (((crossHairPoint.X >= (x - (base.LineWidth * 5))) && (crossHairPoint.X <= (x + (base.LineWidth * 5)))) && (((crossHairPoint.Y >= (num - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num + (base.LineWidth * 5)))) || ((crossHairPoint.Y >= (num2 - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num2 + (base.LineWidth * 5))))))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count != 0)
            {
                PaintLineBase.PointValue value2 = pList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = pList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                double num7 = 0.0;
                if ((num6 - x) != 0f)
                {
                    num7 = (num2 - num) / (num6 - x);
                    double num8 = num - (num7 * x);
                    return new double[] { num7, num8 };
                }
            }
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num2 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num3 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num5 = (num2 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num4 - num3);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num7 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num8 = (base.CgInstance.GetRecordByTime(value2.Key) + (num7 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num9 = (base.CgInstance.GetRecordByTime(value3.Key) + (num7 - indexByPoint)) - 1;
                            if (num8 >= 0)
                            {
                                if (num8 > (base.CgInstance.LastVisibleRecord - 1))
                                {
                                    num8 = base.CgInstance.LastVisibleRecord - 1;
                                }
                            }
                            else
                            {
                                num8 = 0;
                            }
                            if (num9 < 0)
                            {
                                num9 = 0;
                            }
                            else if (num9 > (base.CgInstance.LastVisibleRecord - 1))
                            {
                                num9 = base.CgInstance.LastVisibleRecord - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num8][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num5);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num5);
                            return;
                        }
                        case 1:
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), base.CgInstance.GetMouseOverValue());
                            return;

                        case 2:
                            base.PointList[1] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), base.CgInstance.GetMouseOverValue());
                            return;
                    }
                }
            }
        }
    }
}

